<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ResizeObserver 使用</title>
  <style>
    .container {
      background-color: red;
      width: 300px;
      height: 300px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
      /* transition: all .5s; */
    }
  </style>
</head>
<body>
  <h1>通过 ResizeObserver 类，监听元素长宽的变化</h1>
  <div class="container" id="div"></div>

  <button id="btn">变大</button>
</body>
<script>
  const $div = document.querySelector('#div')
  document.querySelector('#btn').onclick = function() {
    const width = $div.offsetWidth
    const height = $div.offsetHeight
    $div.style.width = (width + 100) + 'px'
    $div.style.height = (height + 100) + 'px'
  }

  const o = new ResizeObserver((a) => {
    const $el = a[0]
    $el.target.innerHTML = $el.contentRect.width + " x " + $el.contentRect.height
  })
  o.observe($div)
</script>
</html>